package com.hitqz.robot.dispatch.netty.service.impl;

import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.api.business.entity.RobotGroupEntity;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotApplyPathDto;
import com.hitqz.robot.dispatch.netty.strategy.AlgorithmStrategy;
import com.hitqz.robot.dispatch.netty.strategy.publicPathStrategy.util.DynamicCalculationUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2025/1/3
 */
@Service("dynamicCalculationStrategy")
@RequiredArgsConstructor
@Slf4j
public class DynamicCalculationService implements AlgorithmStrategy {

    private static ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(
            200,
            200,
            60,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(200),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.DiscardOldestPolicy());



    @Override
    public void changePath(RobotGroupEntity robotGroup, RobotApplyPathDto pathDto) {
        threadPoolExecutor.submit(()-> {
            DynamicCalculationUtil.submitPath(robotGroup, pathDto);
        });
    }


    @Override
    public void calculate(int groupId, List<RobotEntity> robotEntityList) {
        threadPoolExecutor.execute(()-> {
            for (RobotEntity robotEntity : robotEntityList) {
                DynamicCalculationUtil.getOrSetPathList(robotEntity.getRobotSn(), null, false);
            }
        });
        threadPoolExecutor.submit(()-> {
            DynamicCalculationUtil.dispatchMonitor(groupId, robotEntityList);
        });
    }

}
